﻿using Common;
using DAO.BLL;
using NHibernate.Criterion;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Web.helper;

namespace Web.Areas.Admin.Controllers
{
    public class LessonController : BaseController
    {
        public ActionResult Index()
        {
            return View();
        }

        #region Lesson
        [HttpPost]
        [AuthorizeFilter]
        public JsonResult GetLessonList(string lessonname, int? seriesid, int? categoryid, int? teacherid, DateTime? startDate, DateTime? endDate, int limit = 30, int offset = 1)
        {
            B_lesson b_lesson = new B_lesson();
            List<Order> order = new List<Order>() { Order.Desc("ID") };
            List<SearchTemplate> st = new List<SearchTemplate>();

            st.Add(new SearchTemplate() { key = "is_del", value = 0, searchType = Common.EnumBase.SearchType.Eq });
            st.Add(new SearchTemplate() { key = "", value = new int[] { offset, limit }, searchType = Common.EnumBase.SearchType.Paging });
            if (seriesid.HasValue) 
            {
                st.Add(new SearchTemplate() { key = "Lessonseries", value = new DAO.BLL.B_lessonseries().Get(seriesid), searchType = Common.EnumBase.SearchType.Eq });
            }
            if (categoryid.HasValue && categoryid != 0)
            {
                var ll = new DAO.BLL.B_lessoncategory().Getlc(Convert.ToInt32(categoryid));
                st.Add(new SearchTemplate() { key = "Lessoncategory", values = ll.ToArray(), searchType = Common.EnumBase.SearchType.In });
            }
            if (teacherid.HasValue)
            {
                st.Add(new SearchTemplate() { key = "Teacher", value = new DAO.BLL.B_teacher().Get(teacherid), searchType = Common.EnumBase.SearchType.Eq });
            }
            if (!string.IsNullOrEmpty(lessonname)) 
            {
                st.Add(new SearchTemplate() { key = "lessonname", value = lessonname, searchType = Common.EnumBase.SearchType.Like });
            }
            if (startDate.HasValue) 
            {
                st.Add(new SearchTemplate() { key = "starttime", value = Convert.ToDateTime(startDate), searchType = Common.EnumBase.SearchType.Ge });
            }
            if (endDate.HasValue)
            {
                st.Add(new SearchTemplate() { key = "starttime", value = Convert.ToDateTime(endDate), searchType = Common.EnumBase.SearchType.Le });
            }
            List<Domain.lesson> list = new List<Domain.lesson>();
            var list_nt = b_lesson.GetList(st, order).ToList();
            list_nt.ForEach(m =>
            {
                m.Lessoncategory.name = new DAO.BLL.B_lessoncategory().GetLcName(m.Lessoncategory.ID);
            });
            var list_nt_count = b_lesson.GetCount(st);
            var total = list_nt_count;
            return Json(new { total = total, rows = list_nt }, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [AuthorizeFilter]
        public JsonResult AddLesson(FormCollection form)
        {
            Common.Json json = new Common.Json();
            DAO.BLL.B_lesson b_lesson = new DAO.BLL.B_lesson();
            Domain.lesson m_lesson = new Domain.lesson();

            m_lesson.lessonname = form["txt_lessonname"];
            if (!string.IsNullOrEmpty(form["txt_startDate"]))
            {
                m_lesson.starttime = Convert.ToDateTime(form["txt_startDate"]);
            }
            m_lesson.fileurl = form["txt_downloadpath"];

            m_lesson.Lessonseries = new DAO.BLL.B_lessonseries().Get(Convert.ToInt32(form["txt_lessonList"]));
            m_lesson.Lessoncategory = new DAO.BLL.B_lessoncategory().Get(Convert.ToInt32(form["txt_lessonType"]));
            m_lesson.Teacher = new DAO.BLL.B_teacher().Get(Convert.ToInt32(form["txt_teacher"]));
            m_lesson.powerlist = form["txt_powerlist"];
            m_lesson.video_path = form["txt_video_path"];
            var res = b_lesson.Save(m_lesson);
            if (res > 0)
            {
                json.msg = "添加成功!";
            }
            else
            {
                json.msg = "添加失败!";
                json.status = -1;
            }

            return Json(json);
        }


        [HttpPost]
        [AuthorizeFilter]
        public JsonResult GetLesson(int id)
        {
            B_lesson b_lesson = new B_lesson();
            var res = b_lesson.Get(id);
            return Json(res);
        }

        [HttpPost]
        [AuthorizeFilter]
        public JsonResult EditLesson(FormCollection form)
        {
            Common.Json json = new Common.Json();
            DAO.BLL.B_lesson b_lesson = new DAO.BLL.B_lesson();
            var m_lesson = b_lesson.Get(Convert.ToInt32(form["id"]));
            m_lesson.lessonname = form["txt_lessonname"];
            if (!string.IsNullOrEmpty(form["txt_startDate"]))
            {
                m_lesson.starttime = Convert.ToDateTime(form["txt_startDate"]);
            }
            m_lesson.fileurl = form["txt_downloadpath"];

            m_lesson.Lessonseries = new DAO.BLL.B_lessonseries().Get(Convert.ToInt32(form["txt_lessonList"]));
            m_lesson.Lessoncategory = new DAO.BLL.B_lessoncategory().Get(Convert.ToInt32(form["txt_lessonType"]));
            m_lesson.Teacher = new DAO.BLL.B_teacher().Get(Convert.ToInt32(form["txt_teacher"]));
            m_lesson.powerlist = form["txt_powerlist"];
            m_lesson.video_path = form["txt_video_path"];
            b_lesson.Update(m_lesson);
            json.msg = "修改成功!";
            return Json(json);
        }
        [HttpPost]
        [AuthorizeFilter]
        public JsonResult DelLesson(string ids)
        {
            Common.Json json = new Common.Json();
            B_lesson b_lesson = new B_lesson();
            foreach (var id in ids.Split(new char[] { ',' }))
            {
                var m_nt = b_lesson.Get(Convert.ToInt32(id));
                m_nt.is_del = 1;
                b_lesson.Update(m_nt);
            }
            json.msg = "成功删除" + ids.Split(new char[] { ',' }).Length + "条记录!";
            return Json(json);
        }
        #endregion
    }
}
